package com.springboot.mybatis.service.threaddemo;

/**
 * @author sunyt31481
 * @date 2020/8/10 9:52
 * @description catch住InterruptedException之后的优先选择：在方法签名中抛出异常
 * 在run()方法中会强制try/catch
 * 即在方法中throws出异常，在run()方法中接收并try-catch处理
 */
public class RightWayStopThreadProd implements Runnable{
    @Override
    public void run() {
        while (true){
            System.out.println("go");
            try {
                throwInMethod();
            } catch (InterruptedException e) {
                System.out.println("保存");
                e.printStackTrace();
            }
        }
    }

    private void throwInMethod() throws InterruptedException{
        Thread.sleep(2000);
    }

    public static void main(String[] args) {
        Thread thread = new Thread(new RightWayStopThreadProd());
        thread.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread.interrupt();
    }
}
